#ifndef QLIB_LIBRARYTREE_H
#define QLIB_LIBRARYTREE_H

#include <QtGui/qtreewidget.h>

#define LABEL_LIBRARY   QT_TRANSLATE_NOOP("LibraryTree", "Library")
#define LABEL_GROUP     QT_TRANSLATE_NOOP("LibraryTree", "Group")
#define LABEL_REFERENCE QT_TRANSLATE_NOOP("LibraryTree", "Reference")

class LibraryModel;
class LibraryTreeItem;
class QedaLibrary;
class QedaComponent;

class LibraryTree : public QTreeWidget
{
    Q_OBJECT
public:
    enum Mode
    {
        LibraryMode = 0,
        GroupMode = 1,
        ReferenceMode = 2
    };

    explicit LibraryTree(Mode mode, LibraryModel *libModel, QWidget *parent = 0);

public slots:
    void filterByCategory(int idx);

signals:
    void categoryAdded(const QString &category);
    void categoryRemoved(const QString &category);
    void componentEditRequested(QedaComponent *component);
    
protected:
    void contextMenuEvent(QContextMenuEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);

private slots:
    void componentCreate();
    void componentProperties();
    void componentRemove();
    void componentEdit();
    void libraryCreate();
    void libraryLoad();
    void libraryRemove();

    void addLibrary(QedaLibrary *lib);
    void addComponents(QedaLibrary *lib, QList<QedaComponent*> components);
    void updateComponent(QedaComponent *component);
    void removeComponent(QedaComponent *component);

private:
    void createHeaders();
    void createActions();
    LibraryTreeItem *findTopLevelItem(const QString &name);
    LibraryTreeItem *newCategoryItem(const QString &name, int itemType);
    void removeComponentItem(LibraryTreeItem *categoryItem, LibraryTreeItem *item);
    bool categoryFilter(QTreeWidgetItem *item);
    bool componentFilter(QTreeWidgetItem *item);

private:
    Mode                             m_mode;
    QHash<const QString, QAction *>  m_actions;
    LibraryModel                    *m_libModel;
    QString                          m_filterCategory;
};

#endif // QLIB_LIBRARYTREE_H
